﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace FileRemane
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void FileHandle(Label La, string[] Files)
        {
            int index = Convert.ToInt32(La.Tag.ToString());
            switch (index)
            {
                case 1:
                    DgnFileOK(Files);
                    break;
                case 2:
                    DgnFileNo(Files);
                    break;
                case 3:
                    DgnFileCopyFileName(Files);
                    break;
                case 4:
                    DgnFileNameRecovey(Files);
                    break;
                case 5:
                    DgnFileCopyFullFileName(Files);
                    break;
                case 6:
                    DgnFileHanldSameName(Files);
                    break;


                default:
                    break;
            }

        }

        private void DgnFileNameRecovey(string[] Files)
        {
            //恢复文件名
            int count = 0;
            foreach (string file in Files)
            {
                if (File.Exists(file))
                {
                    FileInfo fileInfo = new FileInfo(file);
                    if (fileInfo.Extension.ToLower() == ".dgn")
                    {
                        //只处理dgn
                        string tempFileName = Path.GetFileName(fileInfo.Name);
                        string newTempFileName = MachFileName(tempFileName);
                        count++;

                        string newFileName = fileInfo.DirectoryName + Path.DirectorySeparatorChar + newTempFileName;
                        fileInfo.MoveTo(newFileName);


                    }
                }
            }
            nof.ShowBalloonTip(5, "提示", string.Format("恢复了{0}个文件名", count), ToolTipIcon.Info);
        }

        private void DgnFileCopyFullFileName(string[] files)
        {
            //复制文件的完整路径
            if (files.Length > 1)
            {
                nof.ShowBalloonTip(5, "提示", "一次只能接受一个文件", ToolTipIcon.Info);
            }
            else
            {
                FileInfo fileInfo = new FileInfo(files[0]);
                Clipboard.SetDataObject(fileInfo.FullName, true);
                nof.ShowBalloonTip(5, "提示", string.Format(@"复制文件 {0} 路径成功", fileInfo.Name), ToolTipIcon.Info);
            }

        }
        /// <summary>
        /// 处理同名文件
        /// </summary>
        /// <param name="Files"></param>
        private void DgnFileHanldSameName(string[] Files)
        {
            //处理同名文件

            FileInfo fileInfo = new FileInfo(Files[0]);
            string hanldPath = fileInfo.DirectoryName;
            string[] files = Directory.GetFiles(hanldPath);
            List<string> listFile = new List<string>();
            listFile.AddRange(files);
            while (listFile.Count > 1)
            {
                string compareFile = listFile[0];

                for (int i = 1; i < listFile.Count; i++)
                {
                    string tempFileName = listFile[i];
                    if (string.Equals(MachFileName(Path.GetFileName(compareFile)), MachFileName(Path.GetFileName(tempFileName))))
                    {
                        //文件相同
                        compareFile = FileCompare(compareFile, tempFileName, ref listFile);


                    }

                }
                listFile.Remove(compareFile);
            }


            nof.ShowBalloonTip(5, "提示", "处理成功", ToolTipIcon.Info);
        }

        private string FileCompare(string AFile, string BFile, ref List<string> listFile)
        {
            //开始比较文件
            /*
             * 1.  A 中包含合格  B中包含合格  比较
             * 2.  A 中包含合格 B 中不包含合格 A 
             * 3.  A 中包含合格 B 没有做处理  A
             *  
             * 4.  A 中包含不合格 B中包含合格
             * 5.  A 中包含不合格 B 中不包含合格
             * 6.  A 中包含不合格 B 没有做处理
             * 
             * 7.  A 没有做处理 B中包含合格
             * 8.  A 没有做处理 B 中不包含合格
             * 9.  A 没有做处理 B 没有做处理
             */

            if (AFile.Contains("检查合格--"))
            {
                if (BFile.Contains("检查合格--"))
                {
                    DateTime dateA = DateTime.Parse(Path.GetFileName(AFile).Substring(0, 10));
                    DateTime dateB = DateTime.Parse(Path.GetFileName(BFile).Substring(0, 10));
                    if (DateTime.Compare(dateA, dateB) > 0)
                    {
                        //A文件更新
                        listFile.Remove(BFile);
                        MoveFile(BFile);
                        return AFile;

                    }
                    listFile.Remove(AFile);
                    MoveFile(AFile);
                    return BFile;

                }

                else
                {
                    listFile.Remove(BFile);
                    MoveFile(BFile);
                    return AFile;

                }

            }
            else if (AFile.Contains("检查合不格--"))
            {
                if (BFile.Contains("检查合格--"))
                {
                    listFile.Remove(AFile);
                    MoveFile(AFile);
                    return BFile;


                }
                else if (BFile.Contains("检查合不格--"))
                {
                    DateTime dateA = DateTime.Parse(Path.GetFileName(AFile).Substring(0, 10));
                    DateTime dateB = DateTime.Parse(Path.GetFileName(BFile).Substring(0, 10));
                    if (DateTime.Compare(dateA, dateB) > 0)
                    {
                        //A文件更新
                        listFile.Remove(BFile);
                        MoveFile(BFile);
                        return AFile;
                    }
                    listFile.Remove(AFile);
                    MoveFile(AFile);
                    return BFile;

                }
                else
                {
                    listFile.Remove(BFile);
                    MoveFile(BFile);
                    return AFile;

                }
            }
            else
            {
                if (BFile.Contains("检查合格--"))
                {
                    listFile.Remove(AFile);
                    MoveFile(AFile);
                    return BFile;

                }
                else
                {
                    listFile.Remove(AFile);
                    MoveFile(AFile);
                    return BFile;
                }
            }


        }
        private void MoveFile(string Filename)
        {
            FileInfo fileInfo = new FileInfo(Filename);
            string hanldPath = fileInfo.DirectoryName;
            string oldPath = hanldPath + Path.DirectorySeparatorChar + "失效文件";
            if (!Directory.Exists(oldPath))
            {
                Directory.CreateDirectory(oldPath);
            }
            int fileCount = Directory.GetFiles(oldPath).Length + 1;
            File.Move(Filename, oldPath + Path.DirectorySeparatorChar + fileCount.ToString() + "--" + fileInfo.Name);
        }
        /// <summary>
        /// 复制文件名
        /// </summary>
        /// <param name="Files"></param>
        private void DgnFileCopyFileName(string[] Files)
        {
            //复制文件信息到粘贴板

            int count = 0;
            string lastString = string.Empty;
            foreach (string file in Files)
            {

                if (File.Exists(file))
                {
                    FileInfo fileInfo = new FileInfo(file);
                    if (fileInfo.Extension.ToLower() == ".dgn")
                    {
                        //只处理dgn
                        count++;
                        string tempFileName = MachFileName(fileInfo.Name);
                        string newtempFileName = tempFileName.Remove(tempFileName.Length - 4, 4) + "\r\n";
                        lastString += newtempFileName;
                    }
                }
            }
            Clipboard.SetDataObject(lastString, true);
            nof.ShowBalloonTip(5, "提示", string.Format("{0}个文件名已复制", count), ToolTipIcon.Info);
        }

        private void DgnFileNo(string[] Files)
        {
            int count = 0;
            foreach (string file in Files)
            {
                if (File.Exists(file))
                {
                    FileInfo fileInfo = new FileInfo(file);
                    if (fileInfo.Extension.ToLower() == ".dgn")
                    {
                        //只处理dgn
                        string tempFileName = Path.GetFileName(fileInfo.FullName);
                        string newTempFileName = DateTime.Now.ToString("yyyy-MM-dd") + " 检查不合格--" + MachFileName(tempFileName);
                        count++;

                        string newFileName = fileInfo.DirectoryName + Path.DirectorySeparatorChar + newTempFileName;
                        fileInfo.MoveTo(newFileName);


                    }
                }
            }
            nof.ShowBalloonTip(5, "提示", string.Format("{0}个文件不合格", count), ToolTipIcon.Info);
        }

        private void DgnFileOK(string[] Files)
        {
            int count = 0;
            foreach (string file in Files)
            {
                if (File.Exists(file))
                {
                    FileInfo fileInfo = new FileInfo(file);
                    if (fileInfo.Extension.ToLower() == ".dgn")
                    {
                        //只处理dgn
                        string tempFileName = Path.GetFileName(fileInfo.FullName);
                        string newTempFileName = DateTime.Now.ToString("yyyy-MM-dd") + " 检查合格--" + MachFileName(tempFileName);
                        count++;

                        string newFileName = fileInfo.DirectoryName + Path.DirectorySeparatorChar + newTempFileName;
                        fileInfo.MoveTo(newFileName);
                    }
                }
            }
            nof.ShowBalloonTip(5, "提示", string.Format("{0}个文件合格", count), ToolTipIcon.Info);
        }

        private void Form1_SizeChanged(object sender, EventArgs e)
        {
            if (WindowState == FormWindowState.Minimized)
            {
                Hide();
            }
        }

        private void nof_DoubleClick(object sender, EventArgs e)
        {
            Visible = true;
            WindowState = FormWindowState.Normal;
        }

        private void label1_DragEnter(object sender, DragEventArgs e)
        {
            //验证是否是文件
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                e.Effect = DragDropEffects.Move;
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }
        }

        private void label1_DragDrop(object sender, DragEventArgs e)
        {
            Label la = (Label)sender;
            string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
            FileHandle(la, files);
        }
        private string MachFileName(string FileName)
        {

            Regex r = new Regex(@"07122.+\.dgn$");
            Match m = r.Match(FileName);
            return m.Value;

        }

        private void label1_DoubleClick(object sender, EventArgs e)
        {
            Process m_Process = new Process();
            m_Process.StartInfo.FileName = "SummaryDate.exe";
            m_Process.Start();
        }
    }
}
